package com.lgx.mall.core.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mall.core.dao.IScoreCardDao;
import com.lgx.mall.core.entity.ScoreCard;

@Component
public class ScoreCardDao  extends GeneralDao implements IScoreCardDao{

	protected Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	
	/**
	 * 创建积分卡
	 */
	@Override
	public ScoreCard createScoreCard(ScoreCard sCard) throws DataException {
		this.getHibernateTemplate().save(sCard);
		log.info("[创建积分卡]:"+sCard.getId());
		return sCard;
	}

	/**
	 * 修改积分卡
	 */
	@Override
	public ScoreCard updateScoreCard(ScoreCard sCard) throws DataException{
		this.getHibernateTemplate().update(sCard);
		log.info("[修改积分卡]:"+sCard.getId());
		return sCard;
	}

	/**
	 * 删除积分卡
	 */
	@Override
	public void deleteScoreCard(ScoreCard sCard) throws DataException{
		log.info("[删除积分卡]:"+sCard.getId());
		this.getHibernateTemplate().delete(sCard);
	}

	/**
	 * 删除指定积分卡
	 * 适用范围，根据ids多项删除调用
	 */
	@Override
	public void deleteScoreCard(Long id) throws DataException{
		this.deleteScoreCard(this.findScoreCardById(id));
		log.info("[删除商品]:"+id);
	}

	/**
	 * 根据id检索积分卡对象
	 */
	@Override
	public ScoreCard findScoreCardById(Long id){
		return this.getHibernateTemplate().get(ScoreCard.class, id);
	}

	/**
	 * 	分页查询积分卡对象
	 */
	@Override
	public Pagination findScoreCardPagination(String card, String scoreStar,
			String scoreEnd, String effectiveTimeStart,
			String effectiveTimeEnd, int pageSize, int pageNum){
		String sql ="select s from ScoreCard s where 1 = 1";   //isDelete=0表示可用，但是状态可能被锁定了 
		
		if(!StringUtils.isEmpty(card)){
			sql += "  and s.card = '%"+card+"%'";
		}
		
		if(scoreStar != null && scoreEnd != null){
			sql += "  and s.score between "+scoreStar+" and "+scoreEnd;
		}
		
		if(!StringUtils.isEmpty(effectiveTimeStart)&&!StringUtils.isEmpty(effectiveTimeEnd)){
			sql += "  and s.effectiveTimeStart >= "+effectiveTimeStart+" and s.effectiveTimeEnd <="+effectiveTimeEnd;
		}
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}

	/**
	 * 根据卡号前缀检索同类型id最大的卡号并返回
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ScoreCard> findsScoreCardbyCard(String card) {
		String sql = "from ScoreCard s where s.card like '"+card+"%' order by s.id desc";
		return this.getHibernateTemplate().find(sql);
	}

	/**
	 * 检查积分卡
	 * and s.effectiveTimeEnd > 2012-16-23
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ScoreCard> checkedScoreCard(String card,String password,String time) {
		String sql = "from ScoreCard s where s.card = '"+card+"' and s.password = '"+password+"'";
		
		if(!StringUtils.isEmpty(time.toString())){
			sql += "  and s.effectiveTimeEnd > '"+time+"'";
		}
		
		sql += " and s.isDelete = '0' and s.locked = 0  order by s.id desc ";

		return this.getHibernateTemplate().find(sql);
	}
}
